package il.ac.technion.wiki.impl.wikidocument.content;

import static org.junit.Assert.fail;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import junit.framework.Assert;

import org.junit.Test;

public class HTMLContentTest {
	String simpleHtml = "<html>simpleTest</html>";
	String html = "<html><head>head</head><body><p>paragraph in the body</p></body></html>";

	@Test
	public void testHTMLContentString() {
		String s = "<not a vaid html";
		try {
			try {
				new HTMLContentImporter(s);
			} catch (IOException e) {
				fail("couldn't create input stream from the string");
			}
			fail("didn't throw exception on invalid html");
		} catch (IllegalArgumentException e) {
		}
		try {
			HTMLContentImporter htmlContent;
			try {
				htmlContent = new HTMLContentImporter(simpleHtml);
				Assert.assertEquals("simpleTest", htmlContent.asString());
				htmlContent = new HTMLContentImporter(html);
				Assert.assertEquals("head paragraph in the body",
						htmlContent.asString());
			} catch (IOException e) {
				System.err.println(e.getMessage());
				fail("didn't parse the string");
			}
		} catch (IllegalArgumentException e) {
			fail("failed on legal html");
		}
	}

	 @Test
	public void testHTMLContentInputStream() {
		InputStream is = null;
		try {
			new HTMLContentImporter(is);
			fail("didn't fail on null argument");
		} catch (Exception e) {
		}
		File f = new File("c:\\test.html");
		if (!f.exists())
			return;
		FileInputStream fis = null;
		try {
			fis = new FileInputStream((f));
		} catch (FileNotFoundException e1) {
			fail("couldn't create file input stream");
		}
		// String line = "";
		// BufferedReader br = new BufferedReader(new
		// InputStreamReader((InputStream)fis));
		// try {
		// while ((line = br.readLine()) != null) {
		// System.out.println(line);
		// }
		// } catch (IOException e) {
		// fail();
		// }

		try {
			HTMLContentImporter hc = new HTMLContentImporter(fis);
			Assert.assertEquals("head paragraph in the body", hc.asString());
		} catch (Exception e) {
			fail(e.getMessage());
		}
	}
}
